package com.igelato.controller.OrderRecharge;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.igelato.response.AppException;
import com.igelato.response.ErrorCodeMsgEnum;
import com.igelato.model.OrderRecharge;
import com.igelato.service.OrderRechargeService;
import com.igelato.controller.OrderRecharge.*;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igelato.response.R;
import com.igelato.utils.Tools;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Description: 充值订单
 * @Author: wht
 * @Date: 2024-01-24
 */
@Slf4j
@RestController
@RequestMapping("/")
@Api(tags = {"操作充值订单" })
public class OrderRechargeController {
@Autowired
OrderRechargeService orderRechargeService;

@ApiOperation("新增一个充值订单")
@PutMapping("/addOneOrderRecharge")
public<T> R<T> addOneOrderRecharge(@RequestBody OrderRechargeAddDto addDto){
    OrderRecharge orderRecharge =new OrderRecharge();
        BeanUtils.copyProperties(addDto, orderRecharge);
        boolean res=orderRechargeService.save(orderRecharge);

        if(res){
        return R.successMsg("添加成功！");
        }else{
        throw new AppException(ErrorCodeMsgEnum.ADD_ERROR);
        }
        }

@ApiOperation("删除一个充值订单")
@ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "充值订单id", dataType = "Integer", required = true)}
)
@DeleteMapping("/deleteOneOrderRecharge/{id}")
public<T> R<T> deleteOneOrderRecharge(@PathVariable("id") Integer id){
        if(id==null){
        throw new AppException(ErrorCodeMsgEnum.REQUEST_PARAMS_ERROR);
        }
        boolean res=orderRechargeService.removeById(id);

        if(res){
        return R.successMsg("删除成功！");
        }else{
        throw new AppException(ErrorCodeMsgEnum.DELETE_ERROR);
        }
        }

@ApiOperation("修改一个充值订单信息")
@PatchMapping("/updateOneOrderRecharge")
public<T> R<T> updateOneOrderRecharge(@RequestBody OrderRechargeUpdateDto updateDto){
    OrderRecharge orderRecharge =new OrderRecharge();
        BeanUtils.copyProperties(updateDto, orderRecharge);
        boolean res=orderRechargeService.updateById(orderRecharge);

        if(res){
        return R.successMsg("修改成功！");
        }else{
        throw new AppException(ErrorCodeMsgEnum.UPDATE_ERROR);
        }
        }

@ApiOperation("通过id获取一个充值订单信息")
@ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "充值订单id", dataType = "Integer", required = true)}
)
@GetMapping("/getOneOrderRecharge/{id}")
public<T> R<T> getOneOrderRecharge(@PathVariable("id") Integer id){
        if(id==null){
        throw new AppException(ErrorCodeMsgEnum.REQUEST_PARAMS_ERROR);
        }
    OrderRecharge orderRecharge=orderRechargeService.getById(id);
        return R.successData(orderRecharge);
        }

@ApiOperation("分页获取充值订单列表")
@PostMapping("/getOrderRechargeList")
public<T> R<T> getOrderRechargeList(@Valid @RequestBody OrderRechargeSearchDto searchDto)throws IllegalAccessException{
        QueryWrapper<OrderRecharge> queryWrapper=new QueryWrapper<>();

        Field[]fields=searchDto.getClass().getDeclaredFields();
        for(Field field:fields){
        String name=field.getName();
        if(name.equals("page_num")||name.equals("page_size"))continue;
        field.setAccessible(true); // 必须将字段设置为可访问
        Object value=field.get(searchDto);
        if(value!=null){
        queryWrapper.eq(name,value);
        }
        }

        Page<OrderRecharge> page=new Page<>(searchDto.getPage_num(),searchDto.getPage_size());
    orderRechargeService.page(page,queryWrapper);

        HashMap<Object, Object> res=new HashMap<>();
        res.put("page_size",page.getSize());
        res.put("page_num",page.getCurrent());
        res.put("total",page.getTotal());
        res.put("list",page.getRecords());
        return R.successData(res);
        }

@ApiOperation("获取所有充值订单列表")
@GetMapping("/getAllOrderRechargeList")
public<T> R<T> getAllOrderRechargeList(){
        List<OrderRecharge> list=orderRechargeService.list();

        return R.successData(list);
        }
        }
